Public Class frmDiscount

    Private Sub butCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butCancel.Click
        Me.Close()
    End Sub

    Private Sub frmDiscount_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim iControl As Control
        For Each iControl In Me.Controls
            If Microsoft.VisualBasic.Left(iControl.Tag, 1) = "F" Then
                Dim iText As TextBox
                iText = iControl
                iText.Text = "0"
                AddHandler iControl.KeyPress, AddressOf txtKeyPressEnter
            End If
        Next
        AddHandler txtCK01.KeyPress, AddressOf clsSupport.txtNumericKeyPress
        AddHandler txtCK02.KeyPress, AddressOf clsSupport.txtNumericKeyPress
        AddHandler txtCK03.KeyPress, AddressOf clsSupport.txtNumericKeyPress
        AddHandler txtThanh_toan.KeyPress, AddressOf clsSupport.txtNumericKeyPress
        AddHandler txtTien01.KeyPress, AddressOf clsSupport.txtNumericKeyPress
        AddHandler txtTien02.KeyPress, AddressOf clsSupport.txtNumericKeyPress
        AddHandler txtTien03.KeyPress, AddressOf clsSupport.txtNumericKeyPress
        AddHandler txtTong_ck.KeyPress, AddressOf clsSupport.txtNumericKeyPress
        AddHandler txtTong_thuc_thu.KeyPress, AddressOf clsSupport.txtNumericKeyPress
        AddHandler txtCK01.Validated, AddressOf txtCK01_valid
        AddHandler txtCK02.Validated, AddressOf txtCK02_valid
        AddHandler txtCK03.Validated, AddressOf txtCK03_valid
        txtThanh_toan.Text = frmMain.txtT_tien_hang.Text
    End Sub
    Public Sub txtKeyPressEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If AscW(e.KeyChar) = 13 Then
            SendKeys.Send("{Tab}")
        End If
    End Sub

    Private Sub butSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSave.Click
        Dim i As Integer
        If txtCK01.Text = "" Then txtCK01.Text = "0"
        If txtCK02.Text = "" Then txtCK02.Text = "0"
        If txtCK03.Text = "" Then txtCK03.Text = "0"
        If optGiam.Checked = True Then
            txtTien01.Text = CType(txtThanh_toan.Text, Decimal) * (100 - CType(txtCK01.Text, Decimal)) / 100
            txtTien02.Text = CType(txtTien01.Text, Decimal) * (100 - CType(txtCK02.Text, Decimal)) / 100
            txtTien03.Text = CType(txtTien02.Text, Decimal) * (100 - CType(txtCK03.Text, Decimal)) / 100
            txtTong_thuc_thu.Text = txtTien03.Text
            txtTong_ck.Text = 100 - (100 * CType(txtTong_thuc_thu.Text, Decimal) / CType(txtThanh_toan.Text, Decimal))
            For i = 0 To frmMain.tblDetail.Count - 1
                If IsDBNull(frmMain.tblDetail(i).Item("chiet_khau")) = True Then
                    frmMain.tblDetail(i).Item("chiet_khau") = CType(txtTong_ck.Text, Decimal)
                Else
                    frmMain.tblDetail(i).Item("chiet_khau") = frmMain.tblDetail(i).Item("chiet_khau") + CType(txtTong_ck.Text, Decimal)
                End If
                frmMain.tblDetail(i).Item("tien_hang") = frmMain.tblDetail(i).Item("so_luong") * frmMain.tblDetail(i).Item("don_gia") * (100 - frmMain.tblDetail(i).Item("chiet_khau")) / 100
            Next
        Else
            txtTien01.Text = CType(txtThanh_toan.Text, Decimal) * (100 + CType(txtCK01.Text, Decimal)) / 100
            txtTien02.Text = CType(txtTien01.Text, Decimal) * (100 + CType(txtCK02.Text, Decimal)) / 100
            txtTien03.Text = CType(txtTien02.Text, Decimal) * (100 + CType(txtCK03.Text, Decimal)) / 100
            txtTong_thuc_thu.Text = txtTien03.Text
            txtTong_ck.Text = 100 - (100 * CType(txtTong_thuc_thu.Text, Decimal) / CType(txtThanh_toan.Text, Decimal))
            If CType(txtTong_ck.Text, Decimal) < 0 Then
                txtTong_ck.Text = -CType(txtTong_ck.Text, Decimal)
            End If
            For i = 0 To frmMain.tblDetail.Count - 1
                If IsDBNull(frmMain.tblDetail(i).Item("chiet_khau")) = True Then
                    frmMain.tblDetail(i).Item("chiet_khau") = CType(txtTong_ck.Text, Decimal)
                Else
                    frmMain.tblDetail(i).Item("chiet_khau") = frmMain.tblDetail(i).Item("chiet_khau") + CType(txtTong_ck.Text, Decimal)
                End If
                frmMain.tblDetail(i).Item("tien_hang") = frmMain.tblDetail(i).Item("so_luong") * frmMain.tblDetail(i).Item("don_gia") * (100 + frmMain.tblDetail(i).Item("chiet_khau")) / 100
            Next
        End If        
        frmMain.grdDetail.Refresh()
        frmMain.UpdateList()
        Me.Close()
    End Sub

#Region "Validation"
    Private Sub txtCK01_valid(ByVal sender As Object, ByVal e As System.EventArgs)
        If txtCK01.Text = "" Then txtCK01.Text = "0"
        If optGiam.Checked = True Then
            txtTien01.Text = CType(txtThanh_toan.Text, Decimal) * (100 - CType(txtCK01.Text, Decimal)) / 100
            txtTong_thuc_thu.Text = txtTien01.Text
        Else
            txtTien01.Text = CType(txtThanh_toan.Text, Decimal) * (100 + CType(txtCK01.Text, Decimal)) / 100
            txtTong_thuc_thu.Text = txtTien01.Text            
        End If
        txtTong_ck.Text = 100 - (100 * CType(txtTong_thuc_thu.Text, Decimal) / CType(txtThanh_toan.Text, Decimal))
        If CType(txtTong_ck.Text, Decimal) < 0 Then
            txtTong_ck.Text = -CType(txtTong_ck.Text, Decimal)
        End If
    End Sub
    Private Sub txtCK02_valid(ByVal sender As Object, ByVal e As System.EventArgs)
        If txtCK01.Text = "" Then txtCK01.Text = "0"
        If txtCK02.Text = "" Then txtCK02.Text = "0"
        If optGiam.Checked = True Then
            txtTien01.Text = CType(txtThanh_toan.Text, Decimal) * (100 - CType(txtCK01.Text, Decimal)) / 100
            txtTien02.Text = CType(txtTien01.Text, Decimal) * (100 - CType(txtCK02.Text, Decimal)) / 100
            txtTong_thuc_thu.Text = txtTien02.Text
        Else
            txtTien01.Text = CType(txtThanh_toan.Text, Decimal) * (100 + CType(txtCK01.Text, Decimal)) / 100
            txtTien02.Text = CType(txtTien01.Text, Decimal) * (100 + CType(txtCK02.Text, Decimal)) / 100
            txtTong_thuc_thu.Text = txtTien02.Text
        End If
        txtTong_ck.Text = 100 - (100 * CType(txtTong_thuc_thu.Text, Decimal) / CType(txtThanh_toan.Text, Decimal))
        If CType(txtTong_ck.Text, Decimal) < 0 Then
            txtTong_ck.Text = -CType(txtTong_ck.Text, Decimal)
        End If
    End Sub
    Private Sub txtCK03_valid(ByVal sender As Object, ByVal e As System.EventArgs)        
        If txtCK01.Text = "" Then txtCK01.Text = "0"
        If txtCK02.Text = "" Then txtCK02.Text = "0"
        If txtCK03.Text = "" Then txtCK03.Text = "0"
        If optGiam.Checked = True Then
            txtTien01.Text = CType(txtThanh_toan.Text, Decimal) * (100 - CType(txtCK01.Text, Decimal)) / 100
            txtTien02.Text = CType(txtTien01.Text, Decimal) * (100 - CType(txtCK02.Text, Decimal)) / 100
            txtTien03.Text = CType(txtTien02.Text, Decimal) * (100 - CType(txtCK03.Text, Decimal)) / 100
            txtTong_thuc_thu.Text = txtTien03.Text
        Else
            txtTien01.Text = CType(txtThanh_toan.Text, Decimal) * (100 + CType(txtCK01.Text, Decimal)) / 100
            txtTien02.Text = CType(txtTien01.Text, Decimal) * (100 + CType(txtCK02.Text, Decimal)) / 100
            txtTien03.Text = CType(txtTien02.Text, Decimal) * (100 + CType(txtCK03.Text, Decimal)) / 100
            txtTong_thuc_thu.Text = txtTien03.Text
        End If       
        txtTong_ck.Text = 100 - (100 * CType(txtTong_thuc_thu.Text, Decimal) / CType(txtThanh_toan.Text, Decimal))
        If CType(txtTong_ck.Text, Decimal) < 0 Then
            txtTong_ck.Text = -CType(txtTong_ck.Text, Decimal)
        End If
    End Sub
#End Region
End Class